package org.my.stock.util;

import clojure.lang.Obj;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.IgnitionEx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DbToDb {

    public static void type_to_stock() throws IgniteCheckedException {
        String springCfgPath = "/Users/chenfei/Documents/Java/my_database/my-stock/src/main/resources/default-config.xml";
        Ignite ignite = IgnitionEx.start(springCfgPath);
        CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>("public_meta");
        cacheCfg.setSqlSchema("PUBLIC");

        IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg);

        String sql = "SELECT symbol, name, type_name FROM public.stock_date";
        SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
        fieldsQuery.setLazy(true);
        Iterator<List<?>> iterator = cache.query(fieldsQuery).iterator();
        while (iterator.hasNext()) {
            List<?> it = iterator.next();
            if (!it.isEmpty()) {
                getData(cache, it.get(0).toString(), it.get(1).toString(), it.get(2).toString());
            }
        }
    }

    public static void getData(IgniteCache<?, ?> cache, String symbol, String name, String type_name) {
        String sql = "insert into public.type_stock (symbol, name, type_name) values ('" + symbol + "', '" + name + "', '" + type_name + "')";
        cache.query(new SqlFieldsQuery(sql)).getAll();
    }

    /**
     * 获取按最近时间间隔的行业数据
     * */
    public static void update_type_date() throws IgniteCheckedException {
        String springCfgPath = "/Users/chenfei/Documents/Java/my_database/my-stock/src/main/resources/default-config.xml";
        Ignite ignite = IgnitionEx.start(springCfgPath);
        CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>("public_meta");
        cacheCfg.setSqlSchema("PUBLIC");

        IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg);

        String sql = "SELECT symbol, name FROM public.stock_type_date";
        SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
        fieldsQuery.setLazy(true);
        Iterator<List<?>> iterator = cache.query(fieldsQuery).iterator();
        while (iterator.hasNext()) {
            List<?> it = iterator.next();
            if (!it.isEmpty()) {
                getData_date(cache, it.get(1).toString(), 5);
            }
        }
    }

    public static void getData_date(IgniteCache<?, ?> cache, String name, int num) {
        String sql = "SELECT symbol FROM public.type_stock WHERE type_name = '"+ name +"'";
        List<List<?>> lst = cache.query(new SqlFieldsQuery(sql)).getAll();
        for (List<?> rs : lst) {
            String stock_symbol = rs.get(0).toString();
            List<Double> lst_rs = getStock(cache, stock_symbol, num);
            // 更新表
            System.out.println(lst_rs);
        }
    }

    public static List<Double> getStock(IgniteCache<?, ?> cache, String symbol, int num) {
        List<Double> lst = new ArrayList<>();
        String sql = "SELECT price, zhangdie, main_fund, main_fund_bf FROM public.df_stock_date WHERE symbol = '"+ symbol +"' ORDER BY stock_date DESC LIMIT 0, " + num;
        List<List<?>> lstrs = cache.query(new SqlFieldsQuery(sql)).getAll();
        if (!lstrs.isEmpty()) {
            List<?> first = lstrs.get(0);
            List<?> last = lstrs.get(lstrs.size() - 1);

            Double first_price = (Double) first.get(0);
            Double last_price = (Double) last.get(0);
            double price = first_price - last_price;
            lst.add(price);

            double zhangdie = first_price / last_price;
            if (first_price < last_price) {
                zhangdie = -(1 - zhangdie);
                lst.add(zhangdie);
            } else {
                zhangdie = zhangdie - 1;
                lst.add(zhangdie);
            }

            double main_fund = 0D;
            double main_fund_bf = 0D;
            for (List<?> r : lstrs) {
                main_fund += (Double) r.get(2);
                main_fund_bf += (Double) r.get(3);
            }
            lst.add(main_fund);
            lst.add(main_fund_bf);
        } else {
            System.out.println("吴大富");
        }
        return lst;
    }
}

































